package com.orangelife.service.propFeePollfee.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.feelog.IFeeLogService;
import com.orangelife.service.feelog.modal.PropFeeLogDto;
import com.orangelife.service.propFeeCommInfo.IPropFeeCommInfoService;
import com.orangelife.service.propFeeComputeRule.IPropFeeComputeRuleService;
import com.orangelife.service.propFeeComputeRule.model.PropFeeComputeRuleDto;
import com.orangelife.service.propFeePollfee.IPropFeePollfeeService;
import com.orangelife.service.propFeePollfee.model.PollfeeEditDto;
import com.orangelife.service.propFeePollfee.model.PropFeePollfeeDto;
import com.orangelife.service.propFeeRuleLinkApartment.IPropFeeRuleLinkApartmentService;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeePollfee类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-25 14:36:53
 * ==============================================================
 */
@Service("propFeePollfeeService")
public class PropFeePollfeeServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeePollfeeService {
	private static Logger logger = Logger
			.getLogger(PropFeePollfeeServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeePollfeeServiceImpl.logger = logger;
	}
	@Resource(name = "feeLogService")
	private IFeeLogService feeLogService;
	@Resource(name = "propFeeComputeRuleService")
	private IPropFeeComputeRuleService propFeeComputeRuleService;
	@Resource(name = "propFeeRuleLinkApartmentService")
	private IPropFeeRuleLinkApartmentService propFeeRuleLinkApartmentService;

	@Resource(name = "propFeeCommInfoService")
	private IPropFeeCommInfoService propFeeCommInfoService;

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeePollfee(PropFeePollfeeDto propFeePollfee)
			throws AppBizException {

		Assert.notNull(propFeePollfee);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeePollfee.getPeriod())) {
			if (propFeePollfee.getPeriod().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "期数长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePollfee.getStatus())) {
			if (propFeePollfee.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"状态（0，为当期，1为往期）长度超出");
			}
		}

		propFeePollfee.setPollfeeID(IdUtil.getuuId());
		propFeePollfee.setItemStat(TypeConst.FLAG_ON);
		propFeePollfee.setTscreate(new Date());
		propFeePollfee.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeePollfeeMapper.insertSelective", propFeePollfee);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeePollfee(PropFeePollfeeDto propFeePollfee)
			throws AppBizException {

		Assert.notNull(propFeePollfee);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeePollfee.getPollfeeID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeePollfeeDto oldPropFeePollfee = this
				.findPropFeePollfeeByID(propFeePollfee.getPollfeeID());

		if (!StringUtils.isBlank(propFeePollfee.getPeriod())) {
			if (propFeePollfee.getPeriod().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "期数长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePollfee.getStatus())) {
			if (propFeePollfee.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"状态（0，为当期，1为往期）长度超出");
			}
		}

		if (oldPropFeePollfee == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeePollfee.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeePollfeeMapper.updateByPrimaryKeySelective",
				propFeePollfee);
	}

	@Override
	public PropFeePollfeeDto findPropFeePollfeeByID(String propFeePollfeeID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeePollfeeID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeePollfeeDto oldPropFeePollfee = this.getSqlSession().selectOne(
				"PropFeePollfeeSearchMapper.selectByPrimaryKey",
				propFeePollfeeID);
		if (oldPropFeePollfee == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeePollfee.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeePollfee;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeePollfeeByLogic(String propFeePollfeeID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeePollfeeID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeePollfeeDto oldPropFeePollfee = this
				.findPropFeePollfeeByID(propFeePollfeeID);

		oldPropFeePollfee.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeePollfee.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeePollfeeMapper.updateByPrimaryKeySelective",
				oldPropFeePollfee);
	}

	@Override
	public List<Map<String, Object>> findPropFeePollfeeByPage(String commID,
			String ruleID, String tsCreate, String search, int pageNo,
			int pageSize) throws AppBizException {

		Map<String, String> lastTime = propFeeCommInfoService
				.findLastDate(commID);
		if (lastTime == null || lastTime.size() <= 0) {
			throw new AppBizException(2, "亲，期数信息");
		}

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleID", ruleID);
		map.put("tsCreate", tsCreate);
		map.put("commID", commID);
		if (!StringUtils.isBlank(search)) {
			search = search.replaceAll("-", "");
		}
		map.put("search", search);
		map.put("lastTime", lastTime.get("lastTime"));
		map.put("nextTime", lastTime.get("nextTime"));
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeePollfeeSearchMapper.findAllPropFeePollfee", map);

		/*
		 * if (list != null && list.size() > 0) { List<String> apartmentIDs =
		 * new ArrayList<String>(); for (Map<String, Object> propFeeMeter :
		 * list) {
		 * apartmentIDs.add(propFeeMeter.get("apartment_id").toString()); }
		 * 
		 * Map<String, Object> paMap = new HashMap<String, Object>();
		 * paMap.put("apartmentIDs", apartmentIDs); List<Map<String, Object>>
		 * comms = com.orangelife.utils.JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doPostSvc(
		 * UrlPath.bauthFindbuilngUntiApaprt, paMap, null)); Map<String, String>
		 * connValue = new HashMap<String, String>(); connValue.put("comm",
		 * "comm"); connValue.put("apartmentType", "apartmentType");
		 * connValue.put("houseName", "houseName");
		 * connValue.put("buildingName", "buildingName"); list =
		 * SplicingUtils.connoList(list, comms, "apartment_id", "apartmentID",
		 * connValue);
		 * 
		 * }
		 */
		return list;
	}

	@Override
	public List<PropFeePollfeeDto> getPropFeePollfeeByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeePollfeeDto> list = this.getSqlSession().selectList(
				"PropFeePollfeeSearchMapper.getAllPropFeePollfeeDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeePollfeeByRelevantID(
			String pollfeeID, String ruleID, String status, String commID,
			String memberID, String apartmentID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pollfeeID", pollfeeID);
		map.put("ruleID", ruleID);
		map.put("status", status);
		map.put("commID", commID);
		map.put("memberID", memberID);
		map.put("apartmentID", apartmentID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeePollfeeSearchMapper.selectPropFeePollfeeDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeePollfeeDto> findPropFeePollfeeByRelevantID(
			String pollfeeID, String ruleID, String status, String commID,
			String linkID, String apartmentID, String period)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pollfeeID", pollfeeID);
		map.put("ruleID", ruleID);
		map.put("status", status);
		map.put("commID", commID);
		map.put("linkID", linkID);
		map.put("apartmentID", apartmentID);
		map.put("period", period);

		List<PropFeePollfeeDto> list = this.getSqlSession().selectList(
				"PropFeePollfeeSearchMapper.findPropFeePollfeeDtoByRelevantID",
				map);
		return list;
	}

	@Override
	public List<PropFeePollfeeDto> bauthfindPropFeePollfee(
			List<String> propFeePollfeeIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeePollfeeDto> list = this.getSqlSession().selectList(
				"PropFeePollfeeSearchMapper.batchFindPropFeePollfee",
				propFeePollfeeIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeePollfeeByMap(
			Map<String, Object> propFeePollfee) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeePollfeeSearchMapper.batchFindPropFeePollfeeByMap",
				propFeePollfee);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeePollfee(List<Map<String, Object>> propFeePollfees)
			throws AppBizException {
		if (propFeePollfees == null || propFeePollfees.size() <= 0) {
			throw new AppBizException(2, "亲，请输入相关的参数");
		}

		List<PropFeePollfeeDto> recordDtos = new ArrayList<PropFeePollfeeDto>();

		for (Map<String, Object> record : propFeePollfees) {

			if (!record.containsKey("ruleID")) {
				throw new AppBizException(2, "亲，规则信息");
			}
			if (!record.containsKey("apartmentID")) {
				throw new AppBizException(2, "亲，规则信息");
			}
			if (!record.containsKey("curAmount")) {
				throw new AppBizException(2, "亲,公摊录入信息");
			}

			if (record.get("pollfeeID") == null) {
				throw new AppBizException(2, "亲，表信息");

			}

			if (record.get("ruleID") == null) {
				throw new AppBizException(2, "亲，规则信息");
			}
			if (record.get("curAmount") == null) {
				throw new AppBizException(2, "亲,本期信息");
			}

			if (record.get("apartmentID") == null) {
				throw new AppBizException(2, "亲,房屋信息");
			}
			PropFeeComputeRuleDto propFeeComputeRuleDto = propFeeComputeRuleService
					.findPropFeeComputeRuleByID(record.get("ruleID").toString());

			List<PropFeePollfeeDto> propFeePollfeeDtos = this
					.findPropFeePollfeeByRelevantID(null,
							null, null,
							propFeeComputeRuleDto.getCommID(), propFeeComputeRuleDto.getInitID(), null, null);

			// if (record.containsKey("pollfeeID")) {
			// if (record.get("ruleID") == null
			// || StringUtils.isBlank(record.get("ruleID").toString())) {
			// PropFeePollfeeDto propFeePollfee = new PropFeePollfeeDto();
			// propFeePollfee.setPollfeeID(IdUtil.getuuId());
			// propFeePollfee.setItemStat(TypeConst.FLAG_ON);
			// propFeePollfee.setTscreate(new Date());
			// propFeePollfee.setTsedit(new Date());
			// propFeePollfee.setAmount(new BigDecimal(record.get(
			// "curAmount").toString()));
			// propFeePollfee
			// .setPeriod(propFeeComputeRuleDto.getCurDate());
			// propFeePollfee.setCommID(propFeeComputeRuleDto.getCommID());
			// propFeePollfee.setCreateuserID(record.get("userID")
			// .toString());
			// propFeePollfee.setApartmentID(record.get("apartmentID")
			// .toString());
			// recordDtos.add(propFeePollfee);
			//
			// }
			//
			// }

			if (record.containsKey("curPollfeeID") == true) {
				/****
				 * 判断本期的数值 存在就更新
				 */
				if (record.get("curPollfeeID") != null
						&& !StringUtils.isBlank(record.get("curPollfeeID")
								.toString())) {
					PropFeePollfeeDto pollfeeDto = this
							.findPropFeePollfeeByID(record.get("curPollfeeID")
									.toString());
					pollfeeDto.setAmount(new BigDecimal(record.get("curAmount")
							.toString()));

					Map<String, String> lastTime = propFeeCommInfoService
							.findLastDate(propFeeComputeRuleDto.getCommID());
					if (lastTime == null || lastTime.size() <= 0) {
						throw new AppBizException(2, "亲，期数信息");
					}
					pollfeeDto.setStatus("0");
				
					pollfeeDto.setPeriod(lastTime.get("nextTime"));
					pollfeeDto.setEdituserID(record.get("userID").toString());
					pollfeeDto.setTsedit(new Date());
					this.editPropFeePollfee(pollfeeDto);

				} else {

					Map<String, String> lastTime = propFeeCommInfoService
							.findLastDate(propFeeComputeRuleDto.getCommID());
					if (lastTime == null || lastTime.size() <= 0) {
						throw new AppBizException(2, "亲，期数信息");
					}

					/***
					 * 不存在就新增当前的房屋
					 */

					PropFeePollfeeDto propFeePollfee = new PropFeePollfeeDto();
					propFeePollfee.setPeriod(lastTime.get("nextTime"));
					propFeePollfee.setPollfeeID(IdUtil.getuuId());
					propFeePollfee.setRuleID(record.get("ruleID").toString());
					
					propFeePollfee.setLinkID(propFeeComputeRuleDto.getInitID());
					propFeePollfee.setItemStat(TypeConst.FLAG_ON);
					propFeePollfee.setTscreate(new Date());
					propFeePollfee.setTsedit(new Date());
					propFeePollfee.setAmount(new BigDecimal(record.get(
							"curAmount").toString()));
					propFeePollfee.setStatus("0");
					propFeePollfee.setCommID(propFeeComputeRuleDto.getCommID());
					propFeePollfee.setCreateuserID(record.get("userID")
							.toString());
					propFeePollfee.setApartmentID(record.get("apartmentID")
							.toString());

					if (propFeePollfeeDtos == null
							|| propFeePollfeeDtos.size() <= 0) {
						recordDtos.add(propFeePollfee);
					} else {
						boolean isFlag = false;
						/***
						 * 判断是否存在
						 */
						for (PropFeePollfeeDto propFeePollfeeDto : propFeePollfeeDtos) {
							if (propFeePollfeeDto.getApartmentID().equals(
									propFeePollfee.getApartmentID())
									&& propFeePollfeeDto.getPeriod().equals(
											propFeePollfee.getPeriod())) {
								isFlag = true;
							}
						}
						if (!isFlag) {
							recordDtos.add(propFeePollfee);
						}
					}

				}
			}
		}

		if (recordDtos != null && recordDtos.size() > 0) {
			return this.getSqlSession().insert(
					"PropFeePollfeeBauthMapper.bauthInsert", recordDtos);
		}
		return 1;
	}

	@Override
	public List<Map<String, Object>> findPropFeePollfee(String commID,
			String ruleID, String tsCreate, String search)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleID", ruleID);
		map.put("tsCreate", tsCreate);
		map.put("commID", commID);
		if (!StringUtils.isBlank(search)) {
			search = search.replaceAll("-", "");
		}
		map.put("search", search);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeePollfeeSearchMapper.findNoPropFeePoolfee", map);

		/*
		 * if (list != null && list.size() > 0) { List<String> apartmentIDs =
		 * new ArrayList<String>(); for (Map<String, Object> propFeeMeter :
		 * list) {
		 * apartmentIDs.add(propFeeMeter.get("apartment_id").toString()); }
		 * 
		 * Map<String, Object> paMap = new HashMap<String, Object>();
		 * paMap.put("apartmentIDs", apartmentIDs); List<Map<String, Object>>
		 * comms = com.orangelife.utils.JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doPostSvc(
		 * UrlPath.bauthFindbuilngUntiApaprt, paMap, null)); Map<String, String>
		 * connValue = new HashMap<String, String>(); connValue.put("comm",
		 * "comm"); connValue.put("apartmentType", "apartmentType");
		 * connValue.put("houseName", "houseName");
		 * connValue.put("buildingName", "buildingName"); list =
		 * SplicingUtils.connoList(list, comms, "apartment_id", "apartmentID",
		 * connValue);
		 * 
		 * }
		 */
		return list;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int bauthaddPropFeePollfee(
			List<PropFeePollfeeDto> pPropFeePollfeeDtos) throws AppBizException {

		List<PropFeePollfeeDto> pollfeeDtos = new ArrayList<PropFeePollfeeDto>();

		/***
		 * 导入本期的数据
		 */
		for (PropFeePollfeeDto propFeePollfee : pPropFeePollfeeDtos) {

			Assert.notNull(propFeePollfee);
			/************************************************
			 * 判断输入参数的长度以及是否能为空
			 ************************************************/
			if (!StringUtils.isBlank(propFeePollfee.getPeriod())) {
				if (propFeePollfee.getPeriod().length() > 32) {
					throw new AppBizException(CodeConst.SYS_CODE_1002, "期数长度超出");
				}
			}
			if (!StringUtils.isBlank(propFeePollfee.getStatus())) {
				if (propFeePollfee.getStatus().length() > 1) {
					throw new AppBizException(CodeConst.SYS_CODE_1002,
							"状态（0，为当期，1为往期）长度超出");
				}
			}
			Map<String, String> lastTime = propFeeCommInfoService
					.findLastDate(propFeePollfee.getCommID());
			if (lastTime == null || lastTime.size() <= 0) {
				throw new AppBizException(2, "亲，期数信息");
			}

			propFeePollfee.setPeriod(lastTime.get("nextTime"));
			propFeePollfee.setPollfeeID(IdUtil.getuuId());
			propFeePollfee.setItemStat(TypeConst.FLAG_ON);
			propFeePollfee.setTscreate(new Date());
			propFeePollfee.setTsedit(new Date());
//			propFeePollfee.setLinkID(propFeePollfee.getRuleID());
			List<PropFeePollfeeDto> pollfeesDtos = this
					.findPropFeePollfeeByRelevantID(null,
							null,
							null,
							propFeePollfee.getCommID(), propFeePollfee.getLinkID(),
							propFeePollfee.getApartmentID(),
							propFeePollfee.getPeriod());
			if (pollfeesDtos == null || pollfeesDtos.size() <= 0) {
				pollfeeDtos.add(propFeePollfee);
			}
			if (pollfeesDtos != null && pollfeesDtos.size() > 0) {
				/***
				 * 更新金额
				 */
				PropFeePollfeeDto pollfee = pollfeesDtos.get(0);
				pollfee.setAmount(propFeePollfee.getAmount());
				this.editPropFeePollfee(pollfee);

			}
		}
		if (pollfeeDtos != null && pollfeeDtos.size() > 0) {
			return this.getSqlSession().insert(
					"PropFeePollfeeBauthMapper.bauthInsert", pollfeeDtos);
		}
		return 1;
	}

	@Override
	public List<Map<String, Object>> getProllfeeByMap(Map<String, Object> params)
			throws AppBizException {
		PageHelper.startPage(Integer.parseInt(params.get("pageIndex").toString()), Integer.parseInt(params.get("pageSize").toString()));
		
		
		
		return this.getSqlSession().selectList(
				"PropFeePollfeeSearchMapper.selectPropFeePollfeeByMap", params);
	}


	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public synchronized int editProllfee(PollfeeEditDto pollfeeEditDto)
			throws AppBizException {
		//propFeeComputeRuleService.
		PropFeeComputeRuleDto rule=null;
		PropFeePollfeeDto propFeePollfeeDto=null;
		Map<String,PropFeeComputeRuleDto> rules=new HashMap<>();
		
		StringBuffer kkk=new StringBuffer();
		 for (PropFeePollfeeDto p : pollfeeEditDto.getPropFeePollfeeDto()) {
			 
			 if(!StringUtils.isBlank(p.getPollfeeID())){
				 propFeePollfeeDto = this.findPropFeePollfeeByID(p.getPollfeeID());
				 kkk.append(propFeePollfeeDto.getApartmentID()+","+p.getPeriod()+",旧值"+propFeePollfeeDto.getAmount()+"新值"+p.getAmount());
				 this.editPropFeePollfee(p);
				 
			 }else{
				 rule=rules.get(p.getRuleID());
				 p.setCommID(pollfeeEditDto.getCommID());
				 
				 if(StringUtils.isBlank(p.getPeriod())){
						throw new AppBizException(2, "期数信息不能为空");	  
					 
					 
				 }
				 if(StringUtils.isBlank(p.getRuleID())){
						throw new AppBizException(2, "规则信息不能为空");	  
					 
					 
				 }
				 if(rule==null){
					 
					 
					 PropFeeComputeRuleDto rule1= propFeeComputeRuleService.findPropFeeComputeRuleByID(p.getRuleID()); 
					 
					 rules.put(p.getRuleID(), rule1) ;
					 rule=rule1;
				 }
				 
				 if(rule==null){
					 
					 throw new AppBizException(2, "规则信息为空");	  
					 
				 }
				p.setLinkID(rule.getInitID());
             List<PropFeePollfeeDto> l= this.findPropFeePollfeeByRelevantID(null, p.getRuleID(), null, pollfeeEditDto.getCommID(),rule.getInitID(), p.getApartmentID(), p.getPeriod());
            if(l!=null&&l.size()>0){
	       p.setPollfeeID(l.get(0).getPollfeeID());
	  	 this.editPropFeePollfee(p);
		 kkk.append(propFeePollfeeDto.getApartmentID()+","+p.getPeriod()+",旧值"+propFeePollfeeDto.getAmount()+"新值"+p.getAmount());

        }else{
        	
      
        	this.addPropFeePollfee(p);
        	
   		 kkk.append(p.getApartmentID()+","+p.getPeriod()+"新增记录"+p.getAmount());

        }
				 
				 
			 }
			 
			
		}
		 feeLogService.addLog(pollfeeEditDto.getCommID(), PropFeeLogDto.TYPE_COMMSELF_MONEY, PropFeeLogDto.OP_COMMSELF_MONEY_UPDATE, pollfeeEditDto.getUerID(),"变更自定义公摊费用",kkk.toString());
		return 1;
	}
}